package com.vancouver.knows.ui.fragment.citynews;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BUtils;
import com.vancouver.knows.ui.activity.ImageViewPagerActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.adapters.RelatedNewsAdapter;
import com.vancouver.knows.ui.view.AutoGridLinearlayout;
import com.vancouver.knows.ui.view.RichWebView;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.Constants;
import com.vancouver.knows.business.bean.CityNewsDetailInfo;

public class CityNewsUserPostMainDetailView {

	@Bind(R.id.title)
	TextView title;
	@Bind(R.id.upvote)
	TextView upvote;
	@Bind(R.id.sourceName)
	TextView sourceName;
	@Bind(R.id.sourceAuthor)
	TextView sourceAuthor;
	@Bind(R.id.pdg)
	TextView pdg;
	@Bind(R.id.content)
	RichWebView content;
	@Bind(R.id.hotCommentLayout)
	LinearLayout hotCommentLayout;
	@Bind(R.id.imgs)
	RelativeLayout imgs;
	@Bind(R.id.relatedNewsLayout)
	LinearLayout relatedNewsLayout;
	@Bind(R.id.autoList)
	AutoGridLinearlayout autoList;

	private CityNewsDetailActivity activity;
	private CityNewsPullUserPostDetailControllerView rootView;
	private CityNewsDetailInfo cityNewsDetailInfo;
	private CommentItemView commentItemView;

	public CityNewsUserPostMainDetailView(CityNewsDetailActivity activity) {
		this.activity = activity;
		this.cityNewsDetailInfo = activity.getInfo();
		rootView = new CityNewsPullUserPostDetailControllerView(activity);
		rootView.init(cityNewsDetailInfo.getId(), activity);
		ButterKnife.bind(this, rootView);
		init();
	}

	private void init() {
		if (BUtils.isCollectionEmpty(cityNewsDetailInfo.getCommentInfoList())) {
			hotCommentLayout.setVisibility(View.GONE);
		} else {
			hotCommentLayout.setVisibility(View.VISIBLE);
		}

		if(BUtils.isCollectionEmpty(cityNewsDetailInfo.getRelatedPostInfoList())){
			relatedNewsLayout.setVisibility(View.GONE);
		}else{
			relatedNewsLayout.setVisibility(View.VISIBLE);
			autoList.setAutoGridAdapter(new RelatedNewsAdapter(activity,1,cityNewsDetailInfo.getRelatedPostInfoList()));
		}

		title.setText(cityNewsDetailInfo.getTitle());
		updateUpvoteLayout();
		if (!StringUtil.isEmptyString(cityNewsDetailInfo.getSourceName())) {
			sourceName.setVisibility(View.VISIBLE);
			SpannableStringBuilder sourceAuthorStr = AppUtils.replaceColor(
					activity.getString(R.string.source_author, cityNewsDetailInfo.getSourceName()),
					cityNewsDetailInfo.getSourceName(), activity.getResources().getColor(R.color.orange));
			sourceName.setText(sourceAuthorStr);

			if (!StringUtil.isEmptyString(cityNewsDetailInfo.getSourceAuthor())) {
				sourceAuthor.setVisibility(View.VISIBLE);
				sourceAuthor.setText(AppUtils.replaceColor(
						activity.getString(R.string.source_upload, cityNewsDetailInfo.getSourceAuthor()),
						cityNewsDetailInfo.getSourceAuthor(), activity.getResources().getColor(R.color.orange)));
			} else {
				sourceAuthor.setVisibility(View.GONE);
			}
		} else {
			sourceName.setVisibility(View.VISIBLE);
			String source = activity.getResources().getString(R.string.vgh_know);
			sourceName.setText(activity.getString(R.string.source_author, source));

			sourceAuthor.setVisibility(View.VISIBLE);
			sourceAuthor.setText(AppUtils.replaceColor(
					activity.getString(R.string.upload_author, cityNewsDetailInfo.getPostAuthor()),
					cityNewsDetailInfo.getPostAuthor(), activity.getResources().getColor(R.color.orange)));
		}

		pdg.setText(AppUtils.getDateStr(activity, cityNewsDetailInfo.getPdg()));

		String contentStr = cityNewsDetailInfo.getContent();
		content.loadHtmlText(contentStr);

		commentItemView = new CommentItemView();
		commentItemView.init(activity, hotCommentLayout, cityNewsDetailInfo.getCommentInfoList(),
				cityNewsDetailInfo.getId());

		final String[] imgsUrls = cityNewsDetailInfo.getUserpostAttachmentImageArray();
		if (imgsUrls == null || imgsUrls.length == 0) {
			imgs.setVisibility(View.GONE);
		} else {
			int viewCount = imgs.getChildCount();
			int imgsCount = imgsUrls.length;
			for (int i = 0; i < viewCount; i++) {
				if (i >= imgsCount) {
					imgs.getChildAt(i).setVisibility(View.GONE);
				} else {
					String url = imgsUrls[i];
					Picasso.with(activity).load(url).centerCrop()
							.resizeDimen(R.dimen.citynews_detail_imgs_size, R.dimen.citynews_detail_imgs_size)
							.into((ImageView) imgs.getChildAt(i));
					final int index = i;
					imgs.getChildAt(i).setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							Intent intent = new Intent(activity, ImageViewPagerActivity.class);
							ArrayList<String> urls = new ArrayList<String>();
							for (String string : imgsUrls) {
								urls.add(string);
							}
							intent.putStringArrayListExtra("urls", urls);
							intent.putExtra("index", index);
							activity.startActivity(intent);
						}
					});
				}
			}
		}
	}

	public CityNewsPullUserPostDetailControllerView getRootView() {
		return rootView;
	}

	public void updateUpvoteLayout() {
		upvote.setText(String.valueOf(cityNewsDetailInfo.getUpvoteCount()));
		if ("Y".equals(cityNewsDetailInfo.getUserUpvoted())) {
			Drawable drawable = activity.getResources().getDrawable(R.drawable.icon_like_active);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			upvote.setCompoundDrawables(null, drawable, null, null);
			upvote.setTextColor(activity.getResources().getColor(R.color.orange));
		} else {
			Drawable drawable = activity.getResources().getDrawable(R.drawable.icon_like);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			upvote.setCompoundDrawables(null, drawable, null, null);
			upvote.setTextColor(activity.getResources().getColor(R.color.common_font));
		}
	}

	public void subHandleMessage(Message msg) {
		switch (msg.what) {
		case Constants.BUSINESS_STATUS_USER_COMMENT_UPVOTE:
		case Constants.BUSINESS_STATUS_USER_DELETE_COMMENT_UPVOTE:
			commentItemView.updateCommentLayout();
			break;
		}
	}

	@OnClick(R.id.upvote)
	public void onUpvoteClick() {
		activity.upvote();
	}

	public void taskMarkRefreshOver(ATaskMark taskMark) {
		if (taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
			rootView.refreshFinish(ARefreshViewController.SUCCEED);
		} else {
			rootView.refreshFinish(ARefreshViewController.FAIL);
		}
		cityNewsDetailInfo = activity.getInfo();
		init();
	}

}
